<?php

/**
 *  This PHP File is the Server Side Script Accepting the Request From Client Side
 *  It both accept the post and get http method
 * 
 * @name Server.PHP
 * @author Kun.Huang  <huangkun85@gmail.com>
 * @version 1.0 {30/5/2013}
 * @LastEditData: 10/6/2013 * 
 */
// the major Controllors and models
require_once 'BLL/Controller_Faculty.php';
require_once 'BLL/Controller_Secretary.php';
require_once 'BLL/Controller_Advisor.php';
require_once 'BLL/Controller_FileManagement.php';
require_once 'BLL/Controller_System.php';
require_once 'Model/ServerResponseEntity.php';
require_once 'Model/UserEntity.php';

/**
 * Must at least one type of the request
 */
if (empty($_POST) && empty($_GET)) {
    redirectToIndex('Wrong Access,Post or GET Data is required');
}

//******************************************************************************************************
//Handle the HTTP GET: From Here below, It handle the HTTP GET Function
//The Parameter [ActionName] is a compulsory parameter  
// 1. Supporting the security Download Method
//******************************************************************************************************
if (!empty($_GET)) {

    if (empty($_GET["ActionName"]) || empty($_GET["SID"])) {
        redirectToIndex('Wrong access, Parameter[ActionName] is required');
        die();
    }

    $requestActionName = $_GET["ActionName"];
    //----------------------------------------------------------
    // The below request SID Parameter to start with the Session
    //----------------------------------------------------------
    $sessionID = $_GET["SID"];
    session_id($sessionID);
    session_start();
    //++++++++++ Start handle request From Here++++++++++++++
    if ($requestActionName == "FileDownload") {
        $FileID = $_GET["FileID"];
        $fileManagement = new Controller_FileManagement();
        $fileManagement->HandleDownloadFile($FileID);
        die();
    }
    echo 'Wrong HTTP GET Accesss';
    die();
}


//******************************************************************************************************
//Handle the HTTP Post : From Here below, It handles the HTTP POST Function
//0.  HTTP Post Auto login Need Field [UserId] 
//1.Fields are Required [requestActionName] 
//2.Fields are Required After Login[sessionID] [carriedData]
//******************************************************************************************************
if (!empty($_POST["UserID"])) {
    $url = sprintf("../Client/index.html?UserID=%s", $_POST["UserID"]);
    header('Location: ' . $url);
    return;
}


//------------Not Auto Login-------------
$requestActionName = $_POST["requestActionName"];
$sessionID = $_POST["sessionID"];
$carriedData = $_POST["carriedData"];
//the request name is a must for each post Data
if (empty($requestActionName)) {
    redirectToIndex('Wrong access, requestActionName is required');
}

//++++++++++ Start the Post Request without Session ID (Without Login) ++++++++++++++
if (empty($sessionID)) {
    //----------------------------------------------------------------------------------------------------------
    // Desciption ： Handle the UserLogin
    // Author: Kun.Huang (a1184863)
    // DateTime: 19/May/2013
    //-----------------------------------------------------------------------------------------------------------
    if ($requestActionName == "Membership_Login") {
        // start from here
        $UserId = $carriedData[0];
        $controller = new Controller_System();
        $res = $controller->ChkLogign($UserId);
        // login fail
        if (empty($res)) {
            $ResponseData = new ServerResponseEntity('', "Membership_Login", 'Login_Fail', '');
            echo json_encode($ResponseData);
            die();
        }
        // have the data and login sucess
        else {
            $UserEntity = new UserEntity();
            $UserEntity->UserID = $res[0]["UserID"];
            $UserEntity->UserName = $res[0]["UserName"];
            $UserEntity->UserEmail = $res[0]["UserEmail"];
            $UserEntity->RoleID = $res[0]["RoleID"];
            $UserEntity->RoleName = $res[0]["RoleName"];
            $UserEntity->SchoolID = $res[0]["SchoolID"];
            $UserEntity->SchoolName = $res[0]["SchoolName"];
            // create the session and insert the user Entity 
            session_start();
            $_SESSION["UserEntity"] = $UserEntity;
            // return the new generated Session ID
            $ResponseData = new ServerResponseEntity(session_id(), "Membership_Login", 'Login_Sucess', $_SESSION["UserEntity"]);
            echo json_encode($ResponseData);
            die();
        }
    }

    //++++++++++ The Request is not accept when reache here,++++++++++++++
    redirectToIndex('Wrong Post Request' + $requestActionName);
}


//++++++++++ Start the Post Request with Session ID (After Login） ++++++++++++++
session_id($sessionID);
session_start();
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle the File Upload 
// Author: Kun.Huang (a1184863)
// DateTime: 19/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Action_File_UpLoad") {
    $fileController = new Controller_FileManagement();
    $res = $fileController->HandleUploadFile();
    if ($res->StoreState) {
        $ResponseData = new ServerResponseEntity(session_id(), "Action_File_UpLoad_Sucess", 'Sucess', $res);
    } else {
        $ResponseData = new ServerResponseEntity(session_id(), "Action_File_UpLoad_Fail", 'Fail', $res);
    }
    echo json_encode($ResponseData);
    die();
}

if ($requestActionName == "CPS_QueryFileList") {
    $ApplicationID = $carriedData[0];
    $HandleSchoolID = $carriedData[1];
    $controller_FileListRequest = new Controller_FileManagement();
    $res = $controller_FileListRequest->getFileList($ApplicationID, $HandleSchoolID);
    $ResponseData = new ServerResponseEntity(session_id(), "CPS_QueryFileList", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}


//++++++++++++++++++++++++++++  Facualty from Here ++++++++++++++++++++++++++++++++++++++++++++++++++
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data for the Left Accordion Bar
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_QuerySummary") {
    $controller_Faculty = new Controller_Faculty();
    $res = $controller_Faculty->QuerySummary();
    $ResponseData = new ServerResponseEntity(session_id(), "Faculty_QuerySummary", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data for the Dropdown Details of  Left Accordion Bar
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_QuerySummaryDetail") {
    $controller_Faculty = new Controller_Faculty();
    $res = $controller_Faculty->QuerySummaryDetail($carriedData[0]);
    $ResponseData = new ServerResponseEntity(session_id(), "Faculty_QuerySummaryDetail", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data Detail for the Tab Bar
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_QueryAccordionDetail") {
    $controller_Faculty = new Controller_Faculty();
    $res = $controller_Faculty->QueryAccordionDetail($carriedData[0], $carriedData[1]);
    $ResponseData = new ServerResponseEntity(session_id(), "Faculty_QueryAccordionDetail", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}

//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Accept Button 
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_AcceptApplication") {
    $controller_Faculty = new Controller_Faculty();
    $entity = new Entity_AcceptApplication();
    $entity->rowApplicationID = $carriedData["rowApplicationID"];
    $entity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $entity->rowStateID = $carriedData["rowStateID"];
    $entity->rowActionUserID = $carriedData["rowActionUserID"];
    $entity->newActionUserID = $carriedData["newActionUserID"];
    $entity->newActionStateID = $carriedData["newActionStateID"];
    $res = $controller_Faculty->AcceptApplication($entity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'Sucess', $res);
    echo json_encode($ResponseData);
}

//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Adding File for supplement request
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_AddFiles") {
    $controller_Faculty = new Controller_Faculty();
    $fileSupplementEntity = new FileSuplementEntity();
    $fileSupplementEntity->rowApplicationID = $carriedData["rowApplicationID"];
    $fileSupplementEntity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $fileSupplementEntity->rowStateID = $carriedData["rowStateID"];
    $fileSupplementEntity->rowActionUserID = $carriedData["rowActionUserID"];
    $fileSupplementEntity->newActionUserID = $carriedData["newActionUserID"];
    $fileSupplementEntity->newActionStateID = $carriedData["newActionStateID"];
    $fileSupplementEntity->newActionComment = $carriedData["newActionComment"];
    $fileSupplementEntity->FileList = $carriedData["FileList"];
    $res = $controller_Faculty->AddFiles($fileSupplementEntity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'sucess', $res);
    echo json_encode($ResponseData);
}

//----------------------------------------------------------------------------------------------------------
// Desciption ：Facualty Create a new Application
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Faculty_CreateNewApplication") {
    $AppEntity = new ApplicationEntity();
    $AppEntity->ApplicationTypeID = $carriedData["ApplicationTypeID"];
    $AppEntity->ApplicationHandleSchoolIDs = $carriedData["ApplicationHandleSchoolIDs"];
    $AppEntity->RefferenceName = $carriedData["RefferenceName"];
    $AppEntity->RefferenceID = $carriedData["RefferenceID"];
    $AppEntity->RefferenceTypeID = $carriedData["RefferenceTypeID"];
    $AppEntity->ApplicationComment = $carriedData["ApplicationComment"];
    $AppEntity->FileList = $carriedData["FileList"];
    $AppEntity->ActionUserID = $_SESSION["UserEntity"]->UserID;
    $controller_Faculty = new Controller_Faculty();
    $res = $controller_Faculty->CreateNewApplication($AppEntity);
    if (!empty($res)) {
        $ResponseData = new ServerResponseEntity(session_id(), "CPS.Faculty.NewApplication.Result", 'App_Create_Sucess', $res);

        // Send Email to the Secretaty
        $controller = new Controller_System();
        $toList = $controller->getEmailToList($AppEntity->ApplicationHandleSchoolIDs, "RoleType02");
        if (count($toList) > 0) {
            $subject = "Notification -New Application ";
            $body = '<p style="color: red">clikck the link here to access the web site</p><hr><br/>';
            AppConfig::SendMail($subject, $body, $toList);
        }
        echo json_encode($ResponseData);
        die();
    } else {
        $ResponseData = new ServerResponseEntity(session_id(), "CPS.Faculty.NewApplication.Result", 'App_Create_Fail', NULL);
        echo json_encode($ResponseData);
        die();
    }
}
//+++++++++++++++++++++++++++++++++End of Facualty++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++  Secetaty Start From Here ++++++++++++++++++++++++++++++++++++++++++++++++++
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data for the Left Accordion Bar
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------

if ($requestActionName == "Secretaty_QuerySummary") {
    $secretatyController = new Controller_Secretary();
    $res = $secretatyController->QuerySummary($carriedData[0]);
    $ResponseData = new ServerResponseEntity(session_id(), "Secretaty_QuerySummary", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data  for the Tab
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Secretaty_QuerySummaryDetail") {
    $secretatyController = new Controller_Secretary();
    $res = $secretatyController->QuerySummaryDetail($carriedData[0], $carriedData[1]);
    $ResponseData = new ServerResponseEntity(session_id(), "Secretaty_QuerySummaryDetail", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Get the Advisor data when Assign an job for Advisor
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Secretaty_GetAdevisorList") {
    $secretatyController = new Controller_Secretary();
    $res = $secretatyController->getAdvisorList();
    $ResponseData = new ServerResponseEntity(session_id(), "Proc_Busi_Secretary_GetAdevisorList", 'Sucess', $res);
    echo json_encode($ResponseData);
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Accept Application button
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Secretaty_AcceptApplication") {
    $secretatyController = new Controller_Secretary();
    $entity = new Entity_AcceptApplication();
    $entity->rowApplicationID = $carriedData["rowApplicationID"];
    $entity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $entity->rowStateID = $carriedData["rowStateID"];
    $entity->rowActionUserID = $carriedData["rowActionUserID"];
    $entity->newActionUserID = $carriedData["newActionUserID"];
    $entity->newActionStateID = $carriedData["newActionStateID"];
    $res = $secretatyController->AcceptApplication($entity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'Sucess', $res);
    echo json_encode($ResponseData);
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Assign Application button
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//-----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Secretaty_AssignAdvisor") {
    $secretatyController = new Controller_Secretary();
    $entity = new Entity_AssignAdvisor();
    $entity->rowApplicationID = $carriedData["rowApplicationID"];
    $entity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $entity->rowStateID = $carriedData["rowStateID"];
    $entity->rowActionUserID = $carriedData["rowActionUserID"];
    $entity->newActionUserID = $carriedData["newActionUserID"];
    $entity->newActionStateID = $carriedData["newActionStateID"];
    $entity->newActionCommnet = $carriedData["newActionCommnet"];
    $entity->newAdvisorID = $carriedData["newAdvisorID"];
    $res = $secretatyController->AssignAdvisor($entity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'Sucess', $res);
    echo json_encode($ResponseData);
}
//+++++++++++++++++++++++++++++++++End of Secretaty ++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++  Advisor Start From Here ++++++++++++++++++++++++++++++++++++++++++++++++++
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data for the Left Accordion Bar
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Advisor_QuerySummary") {
    $advisorController = new Controller_Advisor();
    $res = $advisorController->QueryApplicationSummary($carriedData[0], $carriedData[1]);
    $ResponseData = new ServerResponseEntity(session_id(), "Advisor_QuerySummary", 'sucess', $res);
    echo json_encode($ResponseData);
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Data for Tab
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Advisor_QueryDetail") {
    $advisorController = new Controller_Advisor();
    $res = $advisorController->QueryApplicationDetail($carriedData[0], $carriedData[1], $carriedData[2]);
    $ResponseData = new ServerResponseEntity(session_id(), "Advisor_QueryDetail", 'sucess', $res);
    echo json_encode($ResponseData);
}
//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle accept button
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Advisor_AcceptApplication") {
    $advisorController = new Controller_Advisor();
    $entity = new Entity_AcceptApplication();
    $entity->rowApplicationID = $carriedData["rowApplicationID"];
    $entity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $entity->rowStateID = $carriedData["rowStateID"];
    $entity->rowActionUserID = $carriedData["rowActionUserID"];
    $entity->rowAdvisorID = $carriedData["rowAdvisorID"]; // extra one
    $entity->newActionUserID = $carriedData["newActionUserID"];
    $entity->newActionStateID = $carriedData["newActionStateID"];
    $res = $advisorController->AcceptApplication($entity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'Sucess', $res);
    echo json_encode($ResponseData);
}

//----------------------------------------------------------------------------------------------------------
// Desciption ： Handle Finilise button
// Author: Kun.Huang (a1184863)
// Email: Huangkun85@gmail.com
// DateTime: 30/May/2013
//----------------------------------------------------------------------------------------------------------
if ($requestActionName == "Advisor_FinaliseApplication") {
    $advisorController = new Controller_Advisor();
    $entity = new Entity_FinaliseApplication();
    $entity->rowApplicationID = $carriedData["rowApplicationID"];
    $entity->rowHandleSchoolID = $carriedData["rowHandleSchoolID"];
    $entity->rowStateID = $carriedData["rowStateID"];
    $entity->rowActionUserID = $carriedData["rowActionUserID"];
    $entity->newActionUserID = $carriedData["newActionUserID"];
    $entity->newActionStateID = $carriedData["newActionStateID"];
    $entity->newActionCommnet = $carriedData["newActionCommnet"];
    $res = $advisorController->FinaliseApplication($entity);
    $ResponseData = new ServerResponseEntity(session_id(), $carriedData["callbackAction"], 'Sucess', $res);
    echo json_encode($ResponseData);
}


if ($requestActionName == "GetApplicationTypeConfig") {
    $SystemConfigController = new Controller_System();
    $res = $SystemConfigController->GetApplicationTypeList();
    echo json_encode($res);
    die();
}

if ($requestActionName == "GetSchoolConfig") {
    $SystemConfigController = new Controller_System();
    $res = $SystemConfigController->GetSchoolList();
    echo json_encode($res);
    die();
}

if ($requestActionName == "GetReferenceTypeConfig") {
    $SystemConfigController = new Controller_System();
    $res = $SystemConfigController->GetRefferenceTypeList();
    echo json_encode($res);
    die();
}


if ($requestActionName == "GetRoleAndSchoolConfig") {
    $SystemConfigController = new Controller_System();
    $res = $SystemConfigController->GetRoleAndSchoolConfig();
    echo json_encode($res);
}

// End OF GetSystem Configuration//////////////////////////////////////////////////////
// For Advanced Search USE
// Author : Yu Hong
// Data: 7/6/2013

if ($requestActionName == "CPS_searchApplicationSchoolList") {
    $controller_System = new Controller_System();
    $res = $controller_System->searchApplicationSL($carriedData[0], $carriedData[1]);
    $ResponseData = new ServerResponseEntity(session_id(), "CPS_searchApplication", 'sucess', $res);
    echo json_encode($ResponseData);
    die();
}

if ($requestActionName == "CPS_searchApplicationActionList") {
    $controller_System = new Controller_System();
    $res = $controller_System->searchApplicationAL($carriedData[0], $carriedData[1]);
    $ResponseData = new ServerResponseEntity(session_id(), "CPS_searchApplication", 'sucess', $res);
    echo json_encode($ResponseData);
    die();
}

if ($requestActionName == "CPS_QueryFileList") {
    $ApplicationID = $carriedData[0];
    $HandleSchoolID = $carriedData[1];
    $controller_FileListRequest = new Controller_FileManagement();
    $res = $controller_FileListRequest->getFileList($ApplicationID, $HandleSchoolID);
    $ResponseData = new ServerResponseEntity(session_id(), "CPS_QueryFileList", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}

if ($requestActionName == "sendEmailByClickingName") {
    $EmailAddress = $carriedData["EmailAddress"];
    $Subject = $carriedData["Subject"];
    $Body = $carriedData["Body"];
    if (AppConfig::SendMail2($Subject, $Body, $EmailAddress)) {
        $ResponseData = new ServerResponseEntity(session_id(), "sendEmailByClickingName", 'Sucess', null);
    } else {
        $ResponseData = new ServerResponseEntity(session_id(), "sendEmailByClickingName", 'Fail', null);
    }
    echo json_encode($ResponseData);
    die();
}

// END Adbanced Search



if ($requestActionName == "CPS_CheckServerNotification") {

    $controller = new Controller_System();
    $res = $controller->getServerNotification();
    $ResponseData = new ServerResponseEntity(session_id(), "CPS_CheckServerNotification", 'Sucess', $res);
    echo json_encode($ResponseData);
    die();
}

/**
 * 
 * @param type $msg
 */
function redirectToIndex($msg) {
    $output = "<script language='javascript' type='text/javascript'>";
    $output.="alert('%s');";
    $output.="window.location.href='../Client/index.html'";
    $output.="</script>";
    echo sprintf($output, $msg);
    die();
}

?>
